package com.ai.service.made.impl;


import com.ai.common.CommonResponse;
import com.ai.entity.function.FunctionBO;
import com.ai.entity.function.FunctionPO;
import com.ai.entity.function.FunctionPage;
import com.ai.entity.function.FunctionVO;

import com.ai.mapper.made.FunctionMapper;
import com.ai.service.made.FunctionService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2020-06-18
 */
@Service
public class FunctionServiceImpl extends ServiceImpl<FunctionMapper, FunctionPO> implements FunctionService {

    @Resource
    FunctionMapper functionMapper;

    @Override
    public CommonResponse oneById(FunctionPO functionPO) {
        return null;
    }

    @Override
    public CommonResponse modifyInfo(FunctionPO functionPO) {
        return null;
    }

    @Override
    public CommonResponse insert(FunctionPO functionPO) {
        return null;
    }

    @Override
    public CommonResponse delete(FunctionPO functionPO) {
        return null;
    }

    @Override
    public CommonResponse selectByPage(FunctionPage page) {
        QueryWrapper<FunctionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper = page.getQuery(queryWrapper);
        IPage<FunctionPO> iPage = page(page, queryWrapper);
        IPage<FunctionVO> voIPage = iPage.convert(FunctionVO::convertPOToVO);
        return CommonResponse.ok(voIPage);
    }

    @Override
    public CommonResponse selectByCamera(FunctionBO functionBO) {
        if (functionBO.getCameraId() == null || functionBO.getCameraId() == 0) {
            return CommonResponse.error("摄像头ID不能为空");
        }
        String nickname = "%";
        if (!StringUtils.isEmpty(functionBO.getNickname())) {
            nickname = "%" + functionBO.getNickname() + "%";
        }

        List<FunctionVO> functionVOS = new ArrayList<>();

        //数据库已存的集合
        List<FunctionVO> checkedList = functionMapper.functionCheckedList(functionBO.getCameraId(), nickname);
        Map<Integer, FunctionVO> checkedMap = new HashMap();
        checkedList.forEach(vo -> {
            JSONArray spot = JSON.parseArray(vo.getSpot());
            Integer key = vo.getId();
            if (checkedMap.containsKey(key)) {
                checkedMap.get(key).getSpots().add(spot);
                checkedMap.get(key).addNum();
            } else {
                vo.setSpots(new JSONArray());
                vo.getSpots().add(spot);
                vo.setNum(1);
                checkedMap.put(key, vo);
            }
            vo.setSpot(null);
        });
        checkedList = checkedMap.values().stream().collect(Collectors.toList());
        functionVOS.addAll(checkedList);

        //数据库未存的集合
        List<FunctionVO> uncheckedList = functionMapper.functionUncheckedList(functionBO.getCameraId(), nickname);
        uncheckedList.forEach(vo -> {
            vo.setState(2);
            vo.setSpots(JSON.parseArray(vo.getSpot()));
            vo.setSpot(null);
        });
        functionVOS.addAll(uncheckedList);
        return CommonResponse.ok(functionVOS);
    }

    @Override
    public String getNameByCode(final String functionType) {
        return functionMapper.getNameByCode(functionType);
    }

    @Override
    public CommonResponse selectAll() {
        QueryWrapper<FunctionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("type");
        return CommonResponse.ok(list(queryWrapper));
    }

    @Override
    public FunctionPO getFunByType(Integer functionType) {
        QueryWrapper<FunctionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",functionType);
        FunctionPO functionPO = this.getOne(queryWrapper);
        return functionPO;
    }
}
